package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baidubce.qianfan.util.http.HttpStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingProject:all";
    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int result=nursingProjectMapper.insert(nursingProject);
        if(result>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return result;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int result = nursingProjectMapper.updateById(nursingProject);

        // 清除缓存
        if (result > 0) {
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }

        return result;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int result = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));

        // 清除缓存
        if (result > 0) {
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }

        return result;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int result = nursingProjectMapper.deleteById(id);

        // 清除缓存
        if (result > 0) {
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }

        return result;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        //从缓存中获取
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        //如果缓存中有数据，直接返回
        if (list != null && !list.isEmpty()) {
            return list;
        }
        //如果缓存中没有数据，从数据库中查询
        list = nursingProjectMapper.getAllProjects();
        //将数据写入缓存
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }

    /**
     * 分页查询护理项目列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo selectPage(Integer pageNum, Integer pageSize) {
        IPage page=new Page(pageNum,pageSize);

        page=page(page,null);
        return getTableDataInfo(page);
    }
    /**
     * 封装分页信息
     * @param page
     * @return
     */
    private TableDataInfo getTableDataInfo(IPage page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("请求成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
